package com.passive.lock.controller.export;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.passive.lock.model.po.TKey;
import com.passive.lock.model.vo.TAuthVo;
import com.passive.lock.model.vo.TDeviceLogVo;
import com.passive.lock.model.vo.TLockVo;
import com.passive.lock.model.vo.TRouteVo;
import com.passive.lock.model.vo.TSysLogVo;
import com.passive.lock.model.vo.TUserVo;
import com.passive.lock.service.auth.AuthService;
import com.passive.lock.service.key.KeyService;
import com.passive.lock.service.lock.LockService;
import com.passive.lock.service.log.DeviceLogService;
import com.passive.lock.service.log.SysLogService;
import com.passive.lock.service.route.RouteService;
import com.passive.lock.service.user.UserService;
import com.passive.lock.utils.Config;
import com.passive.lock.utils.PageUtil;
import com.passive.lock.utils.ResourcesUtil;
import com.passive.lock.utils.Result;

@Controller
@RequestMapping("export")
public class ExportController {
	@Autowired
	private UserService userService;

	@Autowired
	private AuthService authService;

	@Autowired
	private KeyService keyService;

	@Autowired
	private LockService lockService;

	@Autowired
	private DeviceLogService deviceLogService;

	@Autowired
	private SysLogService sysLogService;

	@Autowired
	private RouteService routeService;

	@RequestMapping(value = "/exportuser")
	@ResponseBody
	public Result exportUser(@RequestParam Integer id, @RequestParam String name, @RequestParam String deptid,
			HttpServletRequest req, HttpServletResponse resp) throws Exception {
		TUserVo tUser = (TUserVo) req.getSession().getAttribute("tUserVo");
		TUserVo tUserVo = new TUserVo();
		tUserVo.setId(id);
		tUserVo.setName(name);
		tUserVo.setGroupid(deptid);
		List<TUserVo> list = userService.exportUser(tUser.getGroupid(), tUserVo);
		return Result.ok(list);

	}

	@RequestMapping(value = "/exportkey")
	@ResponseBody
	public Result exportKey(@RequestParam(required = false) String deptId,
			@RequestParam(required = false) String keyName, @RequestParam(required = false) String userName,
			@RequestParam(required = false) String keyNote, @RequestParam(required = false) String keyState,
			HttpServletRequest req, HttpServletResponse resp) {
		try {
			TUserVo tUser = (TUserVo) req.getSession().getAttribute("tUserVo");
			List<TKey> list = keyService.keyList(tUser.getGroupid(), deptId, keyName, userName, keyNote, keyState, -1,
					-1);
			return Result.ok(list);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}

	}

	@RequestMapping(value = "/exportlock")
	@ResponseBody
	public Result exportLock(@RequestParam(required = false) String deptid,
			@RequestParam(required = false) String lockName, @RequestParam(required = false) String lockCode,
			@RequestParam(required = false) String lockType, HttpServletRequest req, HttpServletResponse resp)
			throws Exception {
		try {
			TUserVo tUserVo = (TUserVo) req.getSession().getAttribute("tUserVo");
			List<TLockVo> list = lockService.lockList(tUserVo.getGroupid(), deptid, lockName, lockCode, lockType, -1,
					-1);
			return Result.ok(list);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}

	@RequestMapping(value = "/exportroute")
	@ResponseBody
	public Result exportRoute(@RequestParam Integer regionid, @RequestParam(required = false) Integer prouteid,
			@RequestParam(required = false) String routename, HttpServletRequest req, HttpServletResponse res)
			throws Exception {
		try {
			TUserVo tUserVo = (TUserVo) req.getSession().getAttribute("tUserVo");
			if (regionid == null || "".equals(regionid)) {
				regionid = tUserVo.getRegionid();
			}
			List<TRouteVo> list = routeService.routeList(regionid, routename, tUserVo.getGroupid(), prouteid, -1, -1);
			return Result.ok(list);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}

	}

	@RequestMapping(value = "/exportauth")
	@ResponseBody
	public Result exportAuth(@RequestParam(required = false) String groupid, @RequestParam long beginTime,
			@RequestParam long endTime, @RequestParam(required = false) String authname,
			@RequestParam(required = false) String username, @RequestParam(required = false) String employeename,
			@RequestParam(required = false) String authtype, HttpServletRequest req, HttpServletResponse resp) {
		try {
			TUserVo tUserVo = (TUserVo) req.getSession().getAttribute("tUserVo");
			if (groupid == null || "".equals(groupid)) {
				groupid = tUserVo.getGroupid();
			}
			List<TAuthVo> list = authService.authList(groupid, beginTime, endTime, authname, username, employeename,
					authtype, -1, -1);
			return Result.ok(list);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}

	}

	@RequestMapping(value = "/exportdevicelog")
	@ResponseBody
	public Result exportDeviceLog(@RequestParam long beginTime, @RequestParam long endTime,
			@RequestParam(required = false) String logType, @RequestParam(required = false) String username,
			@RequestParam(required = false) String lockname, @RequestParam(required = false) String keyname,
			HttpServletRequest req, HttpServletResponse resp) {
		try {
			TUserVo tUserVo = (TUserVo) req.getSession().getAttribute("tUserVo");
			List<TDeviceLogVo> list = deviceLogService.deviceLogList(tUserVo.getGroupid(), beginTime, endTime, logType,
					username, lockname, keyname, -1, -1);
			return Result.ok(list);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}

	@RequestMapping(value = "/exportsyslog")
	@ResponseBody
	public Result exportSysLog(@RequestParam long beginTime, @RequestParam long endTime,
			@RequestParam(required = false) String logType, @RequestParam(required = false) String username,
			HttpServletRequest req, HttpServletResponse resp) throws Exception {
		try {
			TUserVo tUserVo = (TUserVo) req.getSession().getAttribute("tUserVo");
			List<TSysLogVo> list = sysLogService.sysLogList(tUserVo.getGroupid(), beginTime, endTime, logType, username,
					-1, -1);
			return Result.ok(list);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}

	}

}
